import {
	isEmpty,
	formatNumber
} from '@/api/productApi/file'

// 提交时，合并费率
export function mergeRate(_this) {
	let feeRateInfoList = [] // 费率列表
	const subRateList = jsonArray(_this.subRateList)
	const applyRateList = jsonArray(_this.applyRateList)
	const rearRndRateList = jsonArray(_this.rearRndRateList)
	const ransomRateList = jsonArray(_this.ransomRateList)
	const manageRate = jsonArray(_this.manageRate)
	const trusteeRate = jsonArray(_this.trusteeRate)
	const salesSerivceRate = jsonArray(_this.salesSerivceRate)
	feeRateInfoList = [ // 无下属基金或下属基金
		...subRateList.splice(0, 4), // 普通认购费率列表
		...applyRateList.splice(0, 4), // 前端-普通申购
		...rearRndRateList.splice(0, 4), // 后端-普通申购
		...ransomRateList.splice(0, 6), // 普通赎回
		...manageRate.splice(0, 1), // 管理费率
		...trusteeRate.splice(0, 1), // 托托管费率
		...salesSerivceRate.splice(0, 1) // 销售费率
	]

	feeRateInfoList.forEach(item => {
		if (!item.feeRate) {
			item.feeRate = ''
		}
	})
	return jsonArray(feeRateInfoList)
}

// 初始化费率数据
export function initZationm(data, _this) {
	data.feeRateInfoList.forEach(item => {
		item.feeRateUnit = item.feeRateUnit ? item.feeRateUnit : ''
		if (item.feeRateType === '5') {
			item.belongFundRatioUnit = item.belongFundRatioUnit ? item.belongFundRatioUnit : ''
		}
	})

	_this.rateInfo = jsonArray(data)
	_this.timePoint = data.accrualPointTime ? data.accrualPointTime.split(',') : []
	_this.manageRate = data.feeRateInfoList.filter(item => item.feeRateType === '9').splice(0, 1)
	_this.trusteeRate = data.feeRateInfoList.filter(item => item.feeRateType === '7').splice(0, 1)
	_this.salesSerivceRate = data.feeRateInfoList.filter(item => item.feeRateType === '8').splice(0, 1)
	_this.subRateList = data.feeRateInfoList.filter(item => item.feeRateType === '1').splice(0, 4)
	_this.applyRateList = data.feeRateInfoList.filter(item => item.feeRateType === '3').splice(0, 4)
	_this.rearRndRateList = data.feeRateInfoList.filter(item => item.feeRateType === '10').splice(0, 4)
	_this.ransomRateList = data.feeRateInfoList.filter(item => item.feeRateType === '5').splice(0, 6)
}

export function jsonArray(list) { // 将数组序列化
	return JSON.parse(JSON.stringify(list))
}

// list 验证的费率数组
// msg 费率非法提示字段
//
export function RateVerification(list, msg, _this) {
	let eFlag = true
	let feeRateFlag = true
	let thanFlag = true
	let startFlag = true
	let belongFundRatioFlag = true
	for (let i = 0; i < list.length; i++) {
		if (i === 0) { // 第一行
			if (!['ransomRateMsg', 'rearRndMsg'].includes(msg)) {
				// 第一行判断区间末位和费率是否为空
				if (isEmpty(list[i].endSection) && isEmpty(list[i].feeRate)) { // 为空
					list[i].startSection = ''
					list[i].feeRate = ''
				} else { // 不为空
					list[i].startSection = '0'
				}
			} else if (isEmpty(list[i].startSection)) {
				startFlag = false
			}
		} else {
			// 上一行区间末位如果为E或者为空，则该行区间始位为空，否则为上一行区别末位值
			list[i].startSection = (list[i - 1].endSection === 'E' || list[i - 1].endSection === '') ? '' : list[i - 1].endSection
			// 循环判断该行的前几行是否有E或者为空
			let flag = false
			for (let j = i; j > 0; j--) {
				if (list[j - 1].endSection === 'E' || isEmpty(list[j - 1].endSection)) {
					list[j].endSection = ''
					list[j].feeRate = ''
					if (['ransomRateMsg', 'rearRndMsg'].includes(msg)) {
						list[j].belongFundRatio = ''
					}
				}
				// 判断前面的行中是否有E
				if (list[j - 1].endSection === 'E') {
					flag = true
				}
				// 判断大小
				if (!['ransomRateMsg', 'rearRndMsg'].includes(msg) && !isEmpty(list[j].startSection) && !isEmpty(list[j].endSection)) {
					if (list[j].endSection === 'E') { // 判断当前行是否等于E
						if (j - 2 >= 0 && Number(list[j - 1].endSection) <= Number(list[j - 2].endSection)) {
							thanFlag = false
						}
					} else {
						if (j - 1 >= 0 && Number(list[j].endSection) <= Number(list[j - 1].endSection)) {
							thanFlag = false
						}
					}
				}
			}
			// 如果没有并且为最后一行并且前一行不为空，则将最后一行区间末位设置为E
			if (i === list.length - 1 && !isEmpty(list[i - 1].endSection) && !flag) {
				list[i].endSection = 'E'
			}
		}
		// 判断是否有正无穷
		if (list[i].endSection === 'E') {
			eFlag = false
		}
		// 判断费率是否为空
		if (!isEmpty(list[i].startSection) && !isEmpty(list[i].endSection) && isEmpty(list[i].feeRate)) {
			feeRateFlag = false
		}
		if (['ransomRateMsg'].includes(msg) && !isEmpty(list[i].startSection) && !isEmpty(list[i].endSection) && !isEmpty(list[i].feeRate) && isEmpty(list[i].belongFundRatio)) {
			belongFundRatioFlag = false
		}
	}
	if (!startFlag) {
		_this[msg] = '第一行区间起始位不能为空！'
	}
	if (!thanFlag) {
		_this[msg] = '区间末位应大于区间起始位！'
	}
	if (!feeRateFlag) {
		_this[msg] = '费率不能为空！'
	}
	if (eFlag) {
		_this[msg] = '正无穷 必填！'
	}
	if (!belongFundRatioFlag) {
		_this[msg] = '归基金资产比例不能为空！'
	}
	// (没有E&&费率为空)||第一行为空则显示信息置为空
	if (
		(!eFlag && feeRateFlag && thanFlag && startFlag && belongFundRatioFlag) ||
		(isEmpty(list[0].startSection) && isEmpty(list[0].endSection) && isEmpty(list[0].feeRate) &&
			(!['ransomRateMsg', 'rearRndMsg'].includes(msg) ||
				(['ransomRateMsg', 'rearRndMsg'].includes(msg) && isEmpty(list[0].belongFundRatio))))) {
		_this[msg] = ''
	}
}

export function valueCheckByRule(list, msg) {
	for (let i = 0; i < list.length; i++) {
		if (!['ransomRateMsg', 'rearRndMsg'].includes(msg) && !!list[i].startSection) {
			const formatValue = formatNumber(list[i].startSection)
			list[i].startSection = formatValue
		}
		if (!['ransomRateMsg', 'rearRndMsg'].includes(msg) && !!list[i].endSection && list[i].endSection !== 'E') {
			const formatValue = formatNumber(list[i].endSection)
			list[i].endSection = formatValue
		}
		if (list[i].feeRate) {
			const formatValue = formatNumber(list[i].feeRate)
			list[i].feeRate = formatValue
		}
	}
}
